<template>
  <div class="orderRev">
    <jc-button
      :btn-arr="['reload', 'newy']"
      :disab-arr="['close', 'cancel', 'using', 'copy', 'freeze', 'com1', 'com2', 'chart']"
      @refresh="getUserList"
      @newly="newly"
    />
    <div class="header">
      <div class="header-name">
        <search
          :options="selectData"
          :search-son="searchSon"
          :msg="tacitly"
          @seek="collect"
          @hand="searchMaterialList"
        />
        <el-button size="mini" type="primary" class="btn" @click="searchMaterialList">
          {{ $t('btn.search') }}
        </el-button>
      </div>
    </div>
    <div class="table-content">
      <jc-table
        v-loading="loading"
        :table-data="tableData"
        :table-header="tableHeader"
        sortable
        serial
      >
        <template v-slot:billSlot="col">
          <el-link type="primary" @click="pushView(col.scope.row.fid)">{{ col.scope.row.fnumber }}</el-link>
        </template>
        <template v-slot:btnSlot="col">
          <el-button size="mini" type="warning" @click="editSalType(col.scope.row)">{{ $t('btn.edit') }}</el-button>
          <el-button size="mini" type="danger" @click="delCustom(col.scope.row.fid)">{{ $t('btn.del') }}</el-button>
        </template>
      </jc-table>
    </div>
    <div class="footer">
      <jc-pagination
        v-show="total > 0"
        :total="total"
        :page.sync="pageNum"
        :limit.sync="size"
        @pagination="getUserList"
      />
    </div>
    <!--拼接配置-->
    <el-dialog
      v-dialog-drag
      :title="$t('market.setting')"
      :visible.sync="addDialogVisible"
      width="400px"
      center
      :close-on-click-modal="false"
      :before-close="addHandleClose"
    >
      <el-form
        ref="ruleForm"
        :model="addRole"
        size="mini"
        label-width="80px"
      >
        <el-form-item :label="$t('shop.name1')">
          <el-input v-model.trim="addRole.fname" />
        </el-form-item>
        <el-form-item :label="$t('shop.type')">
          <el-select v-model="addRole.ftype" filterable>
            <el-option
              v-for="iten in staffDate"
              :key="iten.fid"
              :label="iten.fname"
              :value="iten.fdescripTion"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="前缀">
          <el-input v-model.trim="addRole.fpreix" />
        </el-form-item>
        <el-form-item label="拼接符">
          <el-input v-model.trim="addRole.fjoincharacter" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addHandleClose">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="addRoleSubmit">{{ $t('btn.save') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  splicingList, splicingCreate, splicingEdit, splicingDelete
} from '@/api/basicManagement/userGroup'
import search from '@/components/Search/index'
import searData from '@/components/Search/mixin'
import jcButton from '@/components/Button'
import autocomplete from '@/views/market/marketManage/createMarkerOrder/components/autocomplete'
import { queryViewItem } from '@/api/marketManage/marketOrder'

export default {
  components: {
    search,
    jcButton
  },
  mixins: [searData, autocomplete],
  data() {
    return {
      addDialogVisible: false, // 明细
      isDisabled: false,
      staffDate: [],
      staffDate2: [],
      addRole: {},
      ftype: 96,
      fbillNo: 'fname',
      total: 0, // 总条目
      pageNum: 1, // 当前页
      size: 200, // 每页显示多少条数据
      tableData: [], // 列表数据
      // 表头数据
      tableHeader: [
        { label: this.$t('shop.fnumber'), type: 'bill', prop: 'fnumber' },
        { label: this.$t('shop.name1'), prop: 'fname' },
        { label: '前缀', prop: 'fpreix' },
        { label: '拼接符', prop: 'fjoincharacter' },
        { label: this.$t('shop.operation'), type: 'btn', width: '250' }
      ]
    }
  },
  created() {
    this.getUserList() // 列表
  },
  methods: {
    // 新增
    newly() {
      this.addDialogVisible = true
      this.queryBilType()
    },
    // 单据类型
    async queryBilType() {
      if (this.staffDate.length > 0) return
      const DATA = {
        viewName: 'JOIN_SETTING_TYPE'
      }
      const { data: RES } = await queryViewItem(DATA)
      this.staffDate = RES.list
    },
    // 拼接配置列表
    async getUserList() {
      this.loading = true
      const DATA = {
        pageNum: this.pageNum,
        pageSize: this.size,
        search: this.searCollData
      }
      const { data: res } = await splicingList(DATA)
      this.tableData = res.list
      this.total = res.total
      this.loading = false
    },
    // 搜索物料
    searchMaterialList(ev) {
      this.pageNum = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.getUserList(ev.detail)
      } else {
        this.getUserList()
      }
    },
    // 删除
    delCustom(fid) {
      this.$confirm('确认要删除么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        splicingDelete({ fid }).then(res => {
          if (res.code === 0) {
            this.$message({ showClose: true, message: res.message, type: 'success' })
            this.getUserList()
          } else {
            this.$confirm(res.message, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'error'
            })
          }
        })
      })
    },
    // 新增 关闭前回调
    addHandleClose() {
      this.addRole = {}
      this.addDialogVisible = false
    },
    // 修改
    editSalType(ev) {
      this.addRole = ev
      this.newly()
    },
    // 详情
    pushView(ev) {
      this.$router.push({ path: `/splicFigDetail/${ev}` })
    },
    // 新增|修改
    addRoleSubmit() {
      const DATA = {
        ...this.addRole
      }
      const api = this.addRole.fid ? splicingEdit : splicingCreate
      api(DATA).then(res => {
        if (res.code === 0) {
          this.getUserList()
          this.addHandleClose()
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.orderRev {
  @include listBom;
  .el-input .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
  .header {
    position: relative;
    .header-name {
      width: 100%;
    }
    .btn {
      transform: translateY(18%);
      margin-left: 450px !important;
    }
  }
}
</style>
